﻿#region License
/*
Copyright (c) 2010, Dilyan Rusev
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list
 of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this 
list of conditions and the following disclaimer in the documentation and/or other 
materials provided with the distribution.

* Neither the name of TU-Sofia nor the names of its contributors may be used to endorse or 
promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

using System.Collections.Generic;
using DbEngine;
using System.Linq;

namespace StoreManagement.Data
{
    /// <summary>
    /// Data layer provider for the StoreManagement UI
    /// </summary>
    public class Provider
    {
        /// <summary>
        /// SQLite database
        /// </summary>
        private Database db;

        /// <summary>
        /// Initializes a new instance of the <see cref="Provider"/> class.
        /// </summary>
        public Provider()
        {
            this.db = Database.Open("data.db");
        }        

        /// <summary>
        /// Create a new property in the data base file
        /// </summary>
        /// <param name="name">Name of the property</param>
        /// <param name="description">Description of the property</param>
        /// <param name="price">Price of the property</param>
        /// <param name="state">State the property is in</param>
        public void CreateProperty(string name, string description, float price, PropertyStates state)
        {
            Property prop = new Property();
            prop.Name = name;
            prop.Description = description;
            prop.Price = price;
            prop.State = state;
            this.db.Create<Property>(prop);            
        }

        /// <summary>
        /// Load all properties from database
        /// </summary>
        /// <returns>Properties that are stored in the database file.</returns>
        public IEnumerable<Property> GetAllProperties()
        {
            return this.db.GetAll<Property>();
        }

        /// <summary>
        /// Load all properties that have a <paramref name="desiredState"/>
        /// </summary>
        /// <param name="desiredState">Filter properties that have this state</param>
        /// <returns>Properties, filtered by a <paramref name="desiredState"/></returns>
        public IEnumerable<Property> GetPropertiesWithState(PropertyStates desiredState)
        {
            var condition = string.Concat("\"State\" = ", (int)desiredState);
            return this.db.GetAllWithCondition<Property>(condition);
        }

        /// <summary>
        /// Load all properties that have a description, containing <paramref name="desc"/>
        /// </summary>
        /// <param name="desc">All or part of the description to filter by.</param>
        /// <returns>Properties, filtered by their description</returns>
        public IEnumerable<Property> GetPropertiesWithDescription(string desc)
        {
            var condition = string.Concat("\"Description\" LIKE '%", desc, "%'");
            return this.db.GetAllWithCondition<Property>(condition);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="partialName"></param>
        /// <returns></returns>
        public IEnumerable<Property> GetPropertiesWithName(string partialName)
        {
            var condition = string.Concat("\"Name\" LIKE '%", partialName, "%'");
            return this.db.GetAllWithCondition<Property>(condition);
        }

        /// <summary>
        /// Load all properties that have <paramref name="partialNameOrDescription"/> in Name or Description
        /// </summary>
        /// <param name="partialNameOrDescription">Part of Name, Description or both.</param>
        /// <returns>Properties, filtered by both name and description</returns>
        public IEnumerable<Property> GetPropertiesWithNameOrDescription(string partialNameOrDescription)
        {
            var condition = string.Concat("\"Name\" LIKE '%", partialNameOrDescription, "%' OR \"Description\" LIKE '%", partialNameOrDescription, "%'");
            return this.db.GetAllWithCondition<Property>(condition);
        }

        /// <summary>
        /// Update a property record in database
        /// </summary>
        /// <param name="propertyToUpdate">Instance of the property to record in database.</param>
        public void UpdateProperty(Property propertyToUpdate)
        {
            this.db.Update<Property>(propertyToUpdate);
        }

        /// <summary>
        /// Delete a property by <paramref name="id"/> from the database
        /// </summary>
        /// <param name="id">ID of the property to delete.</param>
        public void DeleteProperty(int id)
        {
            this.db.Delete<Property>(id);
        }
    }
}
